public int[] searchRange(int[] nums, int target) { int first = binarySearch(nums, target); int last = binarySearch(nums, target + 1) - 1; if (first == nums.length || nums[first] != target) return new int[]{-1, -1}; else return new int[]{first, Math.max(first, last)}; }
private int binarySearch(int[] nums, int target) { int l = 0, h = nums.length; // 注意 h 的初始值 while (l < h) { int m = l + (h - l) / 2; if (nums[m] >= target) h = m; else l = m + 1; } return l; }
class Solution: # @param A, a list of integers # @param target, an integer to be searched # @return a list of length 2, [index1, index2] def searchRange(self, A, target): left = 0; right = len(A) - 1 while left <= right: mid = (left + right) / 2 if A[mid] > target: right = mid - 1 elif A[mid] < target: left = mid + 1 else: list = [0, 0] if A[left] == target: list[0] = left if A[right] == target: list[1] = right for i in range(mid, right+1): if A[i] != target: list[1] = i - 1; break for i in range(mid, left-1, -1): if A[i] != target: list[0] = i + 1; break return list return [-1, -1]